package com.jhrc.portal.ws.runner;


import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.jhrc.portal.ws.ClientInstance;
import com.jhrc.portal.ws.NettyWebsocketClient;
import com.jhrc.portal.ws.WebsocketClientInstance;
import com.jhrc.portal.ws.WebsocketConfig;
import com.jhrc.portal.ws.message.DefaultReceiveMessage;
import com.jhrc.portal.ws.message.Msg;
import com.jhrc.portal.ws.message.ReceiveMessage;
import com.jhrc.portal.ws.message.Sender;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**【让WebsocketClient第一次启动】
 * @author luanxd
 */
@Component
public class WebSocketApplicationRunner4nettyclient implements ApplicationRunner {

    private final static Logger logger = LoggerFactory.getLogger(WebSocketApplicationRunner4nettyclient.class);

    @Value("${websocket.url}")
    private String websocketUrl;


    @Override
    public void run(ApplicationArguments args) throws Exception {
        if (StrUtil.isNotEmpty(websocketUrl)) {
            logger.info("开始启动netty websocket client....");

            /**
             * 必须开一个线程（不开这个线程会影响整个springboot的启动）
             */
            new Thread(()-> {


                WebsocketConfig config = new WebsocketConfig();
                config.setHost("172.16.201.16");
                config.setPort(8999);
                config.setPath("websocket");

                Map<String, Object> params = new HashMap<String, Object>();
                params.put("loginType", "1");
                params.put("loginMethod", "2");
                params.put("user", "luanxd");

                config.setSuffixParams(params);
                config.setAutoRebootClient(true);

                config.setKeepAlive(true);
                config.setAutoRebootClient(true);
                config.setReaderIdleTimeSeconds(3);

                ReceiveMessage callback = new DefaultReceiveMessage();

                NettyWebsocketClient client = new NettyWebsocketClient(config, callback);
                ClientInstance clientInstance = new WebsocketClientInstance(client);

                clientInstance.connect();


//                Sender sender = clientInstance.getSender();
//
//                for(int i=0;i<10000;i++){
//                    try {
//                        TimeUnit.SECONDS.sleep(4);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//
//                    Msg msg = new Msg();
//                    msg.setType(1);
//                    msg.setMsg("你好哈哈哈哈哈哈、。。你看什么看，你是第"+i+"个笨蛋！！！");
//                    String jsonStr = JSONObject.toJSONString(msg);
//
//                    sender.send(jsonStr);
//                }

            }).start();

            logger.info("启动netty websocket client完成....");
        } else {
            logger.info("启动netty websocket client失败....，由于客户端要连接websocket.url为空");
        }
    }
}
